package com.syqc.monitor.ui.cost

import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import autodispose2.autoDispose
import com.amap.api.col.`3nl`.it
import com.syqc.adapter.ChoosePhotoAdapter
import com.syqc.comlib.custom.timer.CustomDatePicker
import com.syqc.comlib.custom.timer.DateFormatUtils
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.rx.filterFast
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentFeeDriverBinding
import com.syqc.monitor.databinding.IncludeDescPhotosBinding
import com.syqc.monitor.databinding.IncludeRecyclerWrapBinding
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.utils.InputUtil
import com.syqc.utils.TimeUtil

class DriverFeeFragment :
    BaseFeeFragment<FragmentFeeDriverBinding>(FragmentFeeDriverBinding::inflate) {
    private lateinit var descBinding: IncludeDescPhotosBinding
    private lateinit var mRecyclerVB: IncludeRecyclerWrapBinding
    private lateinit var sureBinding: IncludeSelectSureBinding
    private val mAdapter by lazy { ChoosePhotoAdapter() }
    private val mVM by viewModels<DriverFeeViewModel>()
    override val mViewModel: BaseFeeViewModel
        get() = mVM
    override val isOil: Boolean
        get() = false
    override fun setDate(type: Int, time: String) {
        when (type) {
            0 -> {
                binding.lrvDateCard.setRightText(time)
                mVM.cardDate = time
            }
            1 -> {
                binding.lrvDateFee.setRightText(time)
                mVM.feeDate = time
            }
        }
    }

    override fun uploadData() {
        mVM.uploadDriver()
    }

    override fun clearData() {
        binding.uetDrvName.setText("")
        binding.uetType.setText("")
        binding.uetCode.setText("")
        binding.uetCard.setText("")
        binding.uetYear.setText("")
        binding.uetMoney.setText("")
        descBinding.editFeeRemark.setText("")
        mVM.clearData()
        binding.lrvDateCard.setRightText(mVM.cardDate)
        binding.lrvDateFee.setRightText(mVM.feeDate)
        binding.nsView.fullScroll(View.FOCUS_UP)
    }

    override fun initView(view: View) {
        descBinding = IncludeDescPhotosBinding.bind(binding.root)
        sureBinding = IncludeSelectSureBinding.bind(binding.root)
        sureBinding.tvSure.setText(R.string.desc_com_sure)
        focusChange(binding.uetMoney.getEditText())
        InputUtil.editTextNested(descBinding.editFeeRemark)

        binding.lrvDateCard.setRightText(mVM.cardDate)
        binding.lrvDateFee.setRightText(mVM.feeDate)

        mRecyclerVB = IncludeRecyclerWrapBinding.bind(binding.root)
        mRecyclerVB.recyclerWrap.layoutManager = GridLayoutManager(requireContext(), 3)
        mRecyclerVB.recyclerWrap.adapter = mAdapter
    }

    override fun initEvent() {
        binding.lrvDateCard.setOnClickListener {
            mVM.dataType = 0
            mDatePicker2.show(mVM.cardDate)
        }
        binding.lrvDateFee.setOnClickListener {
            mVM.dataType = 1
            mDatePicker2.show(mVM.feeDate)
        }
        binding.uetDrvName.setOnTextChange {
            mVM.driverName = it
        }
        binding.uetType.setOnTextChange {
            mVM.driverType = it
        }
        binding.uetCode.setOnTextChange {
            mVM.driverCode = it
        }
        binding.uetCard.setOnTextChange {
            mVM.idCard = it
        }
        binding.uetYear.setOnTextChange {
            mVM.annual = it
        }
        binding.uetMoney.setOnTextChange {
            val str = isNumOk(binding.uetMoney, it)
            mVM.money = str
        }

        sureBinding.tvSure.filterFast {
            mVM.uploadDriver()
        }

        descBinding.editFeeRemark.doAfterChanged {
            mVM.remark = it
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val path = mAdapter.data[position]
            if (path.isEmpty()) {
                takePhoto()
            }
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            mViewModel.removePhoto(position)
        }
    }

    override fun initData() {
        super.initData()
        mVM.handlePhotos(null)

        mVM.photoLive.observe(this) {
            mAdapter.setList(it)
            mAdapter.notifyDataSetChanged()
        }
    }

    private val mDatePicker2: CustomDatePicker by lazy {
        CustomDatePicker(
            requireContext(), callback2, TimeUtil.simpleDate,
            TimeUtil.simpleDateEnd, false
        ).apply {
            setCanShowPreciseTime(false)
        }
    }
    private val callback2: CustomDatePicker.Callback = object : CustomDatePicker.Callback {
        override fun onTimeSelected(timestamp: Long) {
            val time: String = DateFormatUtils.long2Str(timestamp, false)
            setDate(mViewModel.dataType, time)
            mDatePicker2.dismiss()
        }
    }

}